package com.snoob.springboot.wConfig.Intercept.auth;

import com.snoob.springboot.base.BaseLoginUserInfo;
import com.snoob.springboot.enums.SysKeyEnum;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @描述 AUTH对象参数解析器，需要在WebMvcConfig中进行配置
 * @作者 lixing
 * @日期 2021/6/11 11:38
 */
public class AuthMethodArgumentResolver implements HandlerMethodArgumentResolver {

	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		// 支持带@AuthInfo注解的参数
		return parameter.hasParameterAnnotation(AuthInfo.class);
	}

	@Override
	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
		BaseLoginUserInfo baseLoginUserInfo = (BaseLoginUserInfo) webRequest.getAttribute(SysKeyEnum.LOGIN_USERINFO.getKey(), NativeWebRequest.SCOPE_REQUEST);
		return baseLoginUserInfo;
	}

}
